package momo.multitree.simulation;

import java.io.FileReader;

import momo.multitree.structure.Graph;
import momo.multitree.structure.Tree;
import momo.multitree.util.WeightCalculator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class OptWeightedTreeFrameWork {
	
	private Log log = LogFactory.getLog(OptWeightedTreeFrameWork.class);
	
	private double lat[] = {WeightCalculator.DEFAULT_LATWEIGHTAGE,	0.4, 0.4, 0.4, 0.50, 0.5, 0.5, 0.60, 0.6, 0.6, 0.70, 0.70, 0.70, 0.8, 0.80, 0.80, 0.90, 0.90, 0.90};
	private double cost[] = {WeightCalculator.DEFAULT_COSTWEIGHTAGE,0.3, 0.4, 0.2, 0.25, 0.4, 0.1, 0.15, 0.3, 0.1, 0.15, 0.07, 0.23, 0.1, 0.05, 0.15, 0.05, 0.01, 0.09};
	private double stab[] = {WeightCalculator.DEFAULT_STABWEIGHTAGE,0.3, 0.2, 0.4, 0.25, 0.1, 0.4, 0.15, 0.1, 0.3, 0.15, 0.23, 0.07, 0.1, 0.15, 0.05, 0.05, 0.09, 0.01};
	
	private int sizes[] = {5, 6, 7, 8, 9, 10, 12, 15, 20, 25, 30, 40, 50};
//	private int sizes[] = {50};
	private String[] filenames;
	
	public OptWeightedTreeFrameWork() {
//		filenames = new String[ sizes.length*100 ];
//		int counter = 0;
//		for(int i=0; i<sizes.length; i++)
//		{
//			for(int j=0; j<100; j++) {
////				filenames[ j + (counter*100) ] = sizes[i]+"_node_symmetric_noleaf_dataset_" + (j+1) +".txt";
//				filenames[ j + (counter*100) ] = sizes[i]+"_node_symmetric_dataset_" + (j+1) +".txt";
//			}
//			counter ++;
//		}
		filenames = new String[]{"50_node_symmetric_dataset_74.txt"};
	}
	
	public void run()
	{
		for(String filename: filenames)
		{
			try
			{
				Graph g = new Graph(new FileReader("dataset/"+filename));
				
				int maxIteration = 100;
				int maxTrial = 100;
				double temperature = 0d;
				double factor = 0.98d;
				OptWeightedTree sim = new OptWeightedTree(g, maxIteration, maxTrial, temperature, factor);
				Tree bestTree = sim.optimizeTree();
				Tree bestLatTree = sim.getBestLatTree();
				Tree bestCostTree = sim.getBestCostTree();
				Tree bestStabTree = sim.getBestStabTree();
//				log.info(filename+", " + bestTree.compWeightedLatency()+", " + bestTree.compCost(false) +", " + bestTree.compStability());
				log.info(filename+", " + this.displayInfo(bestLatTree, bestCostTree, bestStabTree, bestTree));
			}catch(Exception e)
			{
				log.error("Error on filename: " + filename);
				e.printStackTrace();
			}
		}
	}
	
	public void runWithWeightage() {
		for(String filename: filenames)
		{
			for ( int i=0; i< lat.length; i++ ) {
				try
				{
					double l = lat[i];
					double c = cost[i];
					double s = stab[i];
					
					Graph g = new Graph(new FileReader("dataset/"+filename));
					
					int maxIteration = 100;
					int maxTrial = 100;
					double temperature = 0d;
					double factor = 0.98d;
					OptWeightedTree sim = new OptWeightedTree(g, maxIteration, maxTrial, temperature, factor, l, c, s);
					Tree bestTree = sim.optimizeTree();
					bestTree.outputVisual(filename+"_"+l+"_"+c+"_"+s);
					Tree bestLatTree = sim.getBestLatTree();
					Tree bestCostTree = sim.getBestCostTree();
					Tree bestStabTree = sim.getBestStabTree();
					log.info(filename+"_"+l+"_"+c+"_"+s+", " + this.displayInfo(bestLatTree, bestCostTree, bestStabTree, bestTree));
				}catch(Exception e)
				{
					log.error("Error on filename: " + filename);
					e.printStackTrace();
				}
			}
		}
	}
	
	public String displayInfo(Tree bestLatTree, Tree bestCostTree, Tree bestStabTree, Tree bestTree)
	{
		return bestLatTree.compWeightedLatency() + ", " +
				bestLatTree.compCost(false) + ", " +
				bestLatTree.compStability() + ", " +
				bestCostTree.compWeightedLatency() + ", " +
				bestCostTree.compCost(false) + ", " +
				bestCostTree.compStability() + ", " +
				bestStabTree.compWeightedLatency() + ", " +
				bestStabTree.compCost(false) + ", " +
				bestStabTree.compStability() + ", " +
				bestTree.compWeightedLatency() + ", " +
				bestTree.compCost(false) + ", " +
				bestTree.compStability();
	}
	
	public static void main(String args[])
	{
		OptWeightedTreeFrameWork app = new OptWeightedTreeFrameWork();
//		app.run();
		app.runWithWeightage();
	}
	
}
